package personal.xjl._23java1studentdemo.mapper;

import org.apache.ibatis.annotations.*;
import personal.xjl._23java1studentdemo.model.Course;
import personal.xjl._23java1studentdemo.model.Student;
import personal.xjl._23java1studentdemo.model.Teacher;

import java.util.List;

@Mapper
public interface TeacherMapper {
    @Select("select * from teacher ")
    @Results({
            @Result(column = "id",property = "id",javaType = Integer.class),
            @Result(column = "course_id", property = "course", javaType = Course.class,
                    one = @One(select = "personal.xjl._23java1studentdemo.mapper.CourseMapper.selectById")),
            @Result(column = "id", property = "courses", javaType = List.class,
                    many = @Many(select = "personal.xjl._23java1studentdemo.mapper.CourseMapper.selectByTeacherId"))
    })
    List<Teacher> selectAll();
    @Delete("delete from teacher where id=#{id}")
    public int deleteById(int id);
    @Insert("insert into teacher(name,password,sex,birthday,course_id,professional) values (#{name},#{password},#{sex},#{birthday}),#{course_id},#{professional}")
    int insert(Teacher teacher);
    @Select("select * from teacher where id=#{id}")
    Teacher selectById(int id);
    @Update("update teacher set name=#{name},password=#{password},sex=#{sex},birthday=#{birthday},course_id=#{course_id},professional=#{professional} where id=#{id}")
    int update(Teacher teacher);
//    @Select("select * from teacher limit #{fromIndex},#{pageSize}")
//    List<Teacher> selectByPage(int fromIndex, int pageSize);
    @Select("select * from teacher limit #{fromIndex},#{pageSize}")
    @Results({
            @Result(column = "id",property = "id",javaType = Integer.class),
            @Result(column = "course_id", property = "course", javaType = Course.class,
                    one = @One(select = "personal.xjl._23java1studentdemo.mapper.CourseMapper.selectById")),
            @Result(column = "id", property = "courses", javaType = List.class,
                    many = @Many(select = "personal.xjl._23java1studentdemo.mapper.CourseMapper.selectByTeacherId"))
    })
    List<Teacher> selectByPage(int fromIndex, int pageSize);

    @Select("select * from teacher where name=#{username} and password=#{password}")
    Teacher selectByNameAndPwd(String username, String password);

    @Select("select * from course where teacher_id = #{teacherId}")
    List<Course> selectByTeacherId(int teacherId);
//    @Select("select * from teacher ")
//    @Results({
//            @Result(column = "course_id",property = "course",javaType = Course.class,
//                    one = @One(select = "personal.xjl._23java1studentdemo.mapper.CourseMapper.selectById"))
//    })
//    List<Teacher> findAll();
}
